<?php
/**
 * tis-tis 文章管理文件
 *
 * @copyright (c)2010 昆明力行软件  保留所有权利。
 * @link http://www.tis-tis.com
 * @Author: XiangJie
 * @version: 1.0.0
 */
require '../init.php';
require TIS_ROOT.'/admin/internal/smarty_config.php';
require TIS_ROOT.'/admin/internal/tools.php';
require TIS_ROOT.'/internal/common.php';
require_once TIS_ROOT.'/internal/mysql.class.php';

$act=isset($_REQUEST['act'])?$_REQUEST['act']:'list';
$DB = MySql::getInstance();
$smarty->assign('tpl_path','templates/default');
/**
 * 获取指定分类下的文章列表。
 *
 * @author XiangJie
 */
if ($act=='list') {
	$id=isset($_REQUEST['id'])?$_REQUEST['id']:0;

	$articlecat = getArticleCategory();
	$article_list = get_category_articles($id);

	$smarty->assign("title", getPageTitle("文章列表"));
	$smarty->assign("articlecat", $articlecat);
	$smarty->assign("article_list", $article_list);

	$smarty->display('article_list.tpl');
}

else if ($act == 'add') {
	$smarty->assign('cat_select',  getArticleCategorySelectList());
	$smarty->assign('form_action', 'insert');

	$smarty->display('article_info.tpl');
}

else if ($act == 'insert') {
	$isUnique = isUnique($DB->table('article'), 'title', $_POST['title'], "and id = '$_POST[article_cat]'");

	if (!$isUnique) {
		$links[0] = "文章标题在相同分类下不能重复,点击<a href='article.php?act=add'>重新添加</a>。";
		tisMessage($links);
	}
	
	$sql = "insert into ".$DB->table('article')." (id, user_id, title, content) values(".
	     "'$_POST[article_cat]','1','$_POST[title]','$_POST[content]')";
	$DB->query($sql);
	
    redirectURL('article.php?act=list');
}

else if ($act == 'edit') {
	$id=$_REQUEST['id'];
	$article=get_article($id);

	$smarty->assign("article", $article);
	$smarty->assign('cat_select',  getArticleCategorySelectList(0, $article['id']));
	$smarty->assign('form_action', 'update');

	$smarty->display('article_info.tpl');
}

else if ($act == 'update') {
	$isUnique = isUnique($DB->table('article'), 'title', $_POST['title'], "and id = '$_POST[article_cat]'");

	if ($_REQUEST['title'] != $_REQUEST['old_article_name']) {
		if (!$isUnique) {
			$links[0] = "文章标题在相同分类下不能重复,点击<a href='article.php?act=edit&id=".$_POST['id']."'>重新编辑</a>。";
			tisMessage($links);
		}
	}

	$sql = "update ".$DB->table("article")." set title='$_POST[title]', content='$_POST[content]', id='$_POST[article_cat]' ".
           "where article_id='$_POST[id]'";
	$DB->query($sql);

	redirectURL('article.php?act=list');
}

else if ($act == 'delete') {
	$id=$_REQUEST['id'];

	$sql = "DELETE from ".$DB->table('article')." where article_id='$id'";
	$DB->query($sql);

	redirectURL('article.php?act=list');
}